package com.scm.controller;

import com.scm.pojo.Category;
import com.scm.pojo.Product;
import com.scm.pojo.Vender;
import com.scm.service.CategoryService;
import com.scm.service.ProductService;
import com.scm.service.VenderService;
import com.scm.util.Page;
import com.scm.util.QueryResult;
import com.scm.util.Result;
import com.scm.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ProductCategoryController
 * @Description TODO
 * @Author Xu
 * @Date 2021/7/8 15:24
 * @Version 1.0
 **/
@RestController
public class ProductCategoryController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private VenderService venderService;



    //产品类别操作----------------------------------------
    @RequestMapping("/categoryList")
    public QueryResult categoryList(Integer currentPage, Integer pageSize, HttpServletRequest request){
        Map<String, Object> params = Page.get(currentPage, pageSize);
        int total = categoryService.countAll();
        List<Category> list = categoryService.findAllCategory(params);
        return QueryResult.ok(total,list);
    }




    @RequestMapping("/deleteCategory")
    public Result deleteCategory(String categoryID){
        //待添加判断----------------------------------------------------------
        List list = productService.findProduct(categoryID);
        if (list.size()==0){
            categoryService.deleteCategory(categoryID);
            return Result.ok(null);
        }else {
            return Result.error("此类商品存在，无法删除，数据保留");
        }

    }
    @RequestMapping("/addCategory")
    public Result addCategory(){
        List<String> list = categoryService.getAllCategory().stream().map(category -> category.getCategoryID()).collect(Collectors.toList());
        int i=1001;
        for (String id: list){
            int i1 = Integer.parseInt(id);
            if (i==i1){
                i++;
            }
        }
        return Result.ok(i);
    }

    @RequestMapping("/addCategory2")
    public Result addCategory2(Category category){
        categoryService.addCategory(category);
        return Result.ok(null);
    }
    @RequestMapping("/updateCategory")
    public Result updateCategory(String categoryID){
            Category category = categoryService.findCategory(categoryID);
           return Result.ok(category);
    }

    @RequestMapping("/updateCategory2")
    public Result updateCategory2(Category category){
        categoryService.updateCategory(category);
        return Result.ok(null);
    }


    //产品操作---------------------------------------------------------

    @RequestMapping("/productList")
    public QueryResult productList(Integer currentPage, Integer pageSize){
        Map<String,Object> params = Page.get(currentPage,pageSize);
        int total=productService.countAll();
        List<Product> list =productService.findAllProduct(params);
        for (Product product:list){
            product.setCategory(categoryService.getCategoryName(product.getCategoryID()));
        }
        return QueryResult.ok(total,list);

    }

    @RequestMapping("/deleteProduct")
    public Result deleteProduct(String productCode){
        Product product = productService.getProduct(productCode);
        if (!product.getPoNum().equals("0")||!product.getSoNum().equals("0")){
            return Result.error("有采购或销售在途无法删除");
        }else {
            productService.deleteProduct(productCode);
        }
        //待添加判断----------------------------------------------------------------

        return Result.ok(null);
    }

    @RequestMapping("/updateProduct")
    public Result updateProduct(String productCode){
        Product product = productService.getProduct(productCode);
        product.setCategoryList(categoryService.getAllCategory());
        return Result.ok(product);
    }
    @RequestMapping("/updateProduct2")
    public Result updateProduct2( Product product){
        productService.updateProduct(product);
        return Result.ok(null);
    }

    @RequestMapping("/addProduct")
    public Result addProduct(){
        //添加第一次访问，返回自动生成id以及类别集合
        List<String> list = productService.getAllProduct().stream().map(Product::getProductCode).collect(Collectors.toList());
        String productCode = Util.getID(list,"p");
        List list1 = categoryService.getAllCategory();
        Product p = new Product();
        p.setCategoryList(list1);
        p.setProductCode(productCode);
        return Result.ok(p);
    }
    @RequestMapping("/addProduct2")
    public Result addProduct2(Product product){
        String str = new SimpleDateFormat("yyyy-MM--dd hh:mm:ss").format (new Date());
        product.setCreateDate(str);
        productService.addProduct(product);
        return Result.ok(null);
    }

    //采购需求产品信息----------------------------------
    @RequestMapping("/getProduct")
    public Result getProduct(){
        List<Product> list = productService.getAllProduct();
        return Result.ok(list);
    }

    @RequestMapping("/findProductID")
    public Result findProductID(String productCode){
        Product product = productService.getProduct(productCode);
        return Result.ok(product);
    }

    @RequestMapping("/findProductName")
    public Result findProductName(String name){
        Product product = productService.getNameProduct(name);
        return Result.ok(product);
    }

    @RequestMapping("getVender")
    public Result getVender(){
        List<Vender> list = venderService.getAllVender();
        return Result.ok(list);
    }

}
